package com.welyo.view.quizz {
	import com.welyo.model.quizz.TopPlayerVO;
	import com.welyo.model.QuizzModel;
	import com.welyo.view.quizz.event.QuizzEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author vu.doan
	 */
	public class TopPlayer extends Sprite {
		
		private var max_player		:int = 10;
		
		private var arrData			:Array;
		private var arrRow			:Array;
		private var urlData			:String;
		
		public function TopPlayer() {
			arrRow = [];
			var itm:MovieClip;
			for (var i:int = 0; i < 10; i++) {
				itm = this.getChildByName("mcItem" + i) as MovieClip;
				if (itm) {
					arrRow.push(itm);
				}
			}
			
			mcInput.txtInput.maxChars = 7;
			
			mcSend.visible = true;
			mcReplay.visible = false;
			mcReplay.buttonMode = true;
			mcReplay.addEventListener(MouseEvent.CLICK, onClickReplayHandler);
			mcSend.buttonMode = true;
			mcSend.addEventListener(MouseEvent.CLICK, onClickSendHandler);
		}
		
		public function init(path:String = "../xml/quizz_top10.xml"):void {
			mcInput.txtInput.text = "";
			urlData = path;
			arrData = [];
			
			var ldr:URLLoader = new URLLoader();
			ldr.addEventListener(Event.COMPLETE, onCompleteLoadDataHandler);
			ldr.load(new URLRequest(path));
		}
		
		private function onCompleteLoadDataHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadDataHandler);
			
			stage.focus = mcInput.txtInput;
			
			var data:XML = new XML(evt.currentTarget.data);
			var itm:TopPlayerVO;
			for (var i:int = 0; i < data.item.length(); i++ ) {
				itm = new TopPlayerVO(new XML(data.item[i]));
				arrData.push(itm);
			}
			
			setDataForRow();
		}
		
		private function setDataForRow():void {
			for (var i:int = 0; i < arrRow.length; i++) {
				if (arrData[i]) {
					arrRow[i].txtName.text = (i + 1) + ". " + (arrData[i] as TopPlayerVO).NamePlayer;
					arrRow[i].txtMark.text = (arrData[i] as TopPlayerVO).MarkPlayer;
				}
			}
		}
		
		public function show():void {
			mcSend.visible = true;
			mcReplay.visible = false;
			visible = true;
			txtName.text = QuizzModel.getIntance().NamePlayerInput;
			txtNameShadow.text = QuizzModel.getIntance().NamePlayerInput;
			txtLabel.text = QuizzModel.getIntance().HightestLabel;
			txtLabelShadow.text = QuizzModel.getIntance().HightestLabel;
		}
		
		public function hide():void {
			visible = false;
		}
		
		private function onClickReplayHandler(evt:MouseEvent):void {
			dispatchEvent(new QuizzEvent(QuizzEvent.GAME_REPLAY));
		}
		
		private function onClickSendHandler(evt:MouseEvent):void {
			dispatchEvent(new QuizzEvent(QuizzEvent.SAVING_GAME));
			mcSend.mouseEnabled = mcSend.mouseChildren = false;
			sendToServer(QuizzModel.getIntance().URLSaveQuizz);
		}
		
		private function sendToServer(path:String = ""):void {
			var ldr:URLLoader = new URLLoader();
			ldr.addEventListener(Event.COMPLETE, onCompleteSendToServerHandler);
			ldr.addEventListener(IOErrorEvent.IO_ERROR, onErrorSendToServerHandler);
			var reg:URLRequest = new URLRequest(path);
			var param:URLVariables = new URLVariables();
			param.mark = QuizzModel.getIntance().TotalMark;
			param.time = QuizzModel.getIntance().TotalTime - QuizzModel.getIntance().TimeRemain;
			param.username = unescape(mcInput.txtInput.text);
			reg.data = param;
			reg.method = URLRequestMethod.POST;
			ldr.load(reg);
		}
		
		private function onErrorSendToServerHandler(evt:IOErrorEvent):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteSendToServerHandler);
			evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onErrorSendToServerHandler);
			
			mcSend.mouseEnabled = mcSend.mouseChildren = true;
			mcSend.visible = false;
			mcReplay.visible = true;
			dispatchEvent(new QuizzEvent(QuizzEvent.SAVE_COMPLETE));
		}
		
		private function onCompleteSendToServerHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteSendToServerHandler);
			evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onErrorSendToServerHandler);
			
			mcSend.mouseEnabled = mcSend.mouseChildren = true;
			mcSend.visible = false;
			mcReplay.visible = true;
			
			init(urlData);
			TweenLite.delayedCall(0.5, onCompleteDelayHandler);
		}
		
		private function onCompleteDelayHandler():void {
			dispatchEvent(new QuizzEvent(QuizzEvent.SAVE_COMPLETE));
		}
	}
}